package SharedCloud
{
	import mx.collections.ArrayCollection;
	
	public class RefCollection
	{
		protected var innerList:ArrayCollection = new ArrayCollection();
		
		public function get length():int
		{
			return innerList.length;
		}
		
		public function getRefAt(index:int):Ref
		{
			return innerList.getItemAt(index) as Ref;
		}
		
		/**
		 * Find the reference that matches the param
		 */
		public function find(ref:Ref):Ref
		{
			for each(var innerRef:Ref in innerList)
			{
				if(innerRef.clientID == ref.clientID)
					if(innerRef.localID == ref.localID)
						if(innerRef.property == ref.property)
							return innerRef;
			}
			return null;
		}
		
		public function add(ref:Ref):void
		{
			innerList.addItem(ref);
		}
		
		/**
		 * Remove the ref that matches the param
		 */
		public function remove(ref:Ref):void
		{
			var actualRef:Ref = find(ref);
			var index:int = innerList.getItemIndex(actualRef);
			if(index >= 0)
				innerList.removeItemAt(index);
		}
		
		public function toArray():ArrayCollection
		{
			return innerList;
		}
	}
}